package DAO;

import java.sql.Connection;
import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import Model.ExameFisico;
import Utils.Conexao;

public class ExameFisicoDAO {
	public static void Insert(ExameFisico exame) throws SQLException
	{
		Connection con = Conexao.getConection();		
		String mysqlCall = "call spc_insert_exame_fisico(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";			
		CallableStatement call = ExameFisicoDAO.setObjectCallableStatement(exame, mysqlCall, con);
		call.execute();
		con.close();		
		call.close();
	}
	
	public static void Update(ExameFisico exame) throws SQLException
	{		
		Connection con = Conexao.getConection();		
		String mysqlCall = "call spc_update_exame_fisico(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";		
		CallableStatement call = ExameFisicoDAO.setObjectCallableStatement(exame, mysqlCall, con) ;
		call.executeUpdate();
		con.close();
		call.close();
	}
	
	public static ArrayList<ExameFisico> GetArrayListExamesFisicos()
	{
		ArrayList<ExameFisico> arrExameFisico = new ArrayList<ExameFisico>();
		try {			
			
			Connection con = Conexao.getConection();
			String mysqlCall = "call spc_select_exames_fisicos()";			
			CallableStatement call = (CallableStatement) con.prepareCall(mysqlCall);			
			ResultSet res = call.executeQuery();
			
			while(res.next())
			{
				ExameFisico exFisico = ExameFisicoDAO.setInstance(res);
				arrExameFisico.add(exFisico);
			}
			
			con.close();
			call.close();
		}
		catch (SQLException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return arrExameFisico;
	}
	
	public static void inativarExameFisico(int idExameFisico) throws SQLException
	{
		Connection con = Conexao.getConection();
		String sqlQuery = "call spc_intativar_exame_fisico(?)";
		CallableStatement call = (CallableStatement) con.prepareCall(sqlQuery);
		call.setInt(1, idExameFisico);
		call.execute();		
		con.close();
		call.close();
	}
	
	public static ExameFisico LoadObjet(int idExame) throws SQLException
	{		
		ExameFisico exFisico = null;
		Connection con = Conexao.getConection();
		String mysqlCall = "call spc_select_exame_fisico_por_id(?)";		
		
		CallableStatement call = (CallableStatement) con.prepareCall(mysqlCall);
		call.setInt(1, idExame);
		ResultSet res = call.executeQuery();		
		
		if(res.next())
			exFisico = ExameFisicoDAO.setInstance(res);			
		
		con.close();
		call.close();
		
		return exFisico;
	}	
	
	private static CallableStatement setObjectCallableStatement(ExameFisico exame, String mysqlCall, Connection con ) throws SQLException
	{				
		CallableStatement call = (CallableStatement) con.prepareCall(mysqlCall);
		call.setInt(1, exame.getFundoOlhos());
		call.setInt(2, exame.getMembroInfNormal());
		call.setInt(3, exame.getMembroInfEdema());
		call.setInt(4, exame.getMembroInfVarizes());
		call.setInt(5, exame.getAusenciaPulso());
		call.setInt(6, exame.getFemurais());
		call.setInt(7, exame.getPopliteos());
		call.setInt(8, exame.getTribalPosterior());
		call.setInt(9, exame.getDorsalisPedis());
		call.setInt(10, exame.getUlceras());
		call.setInt(11, exame.getAucPulmonarMv());
		call.setInt(12, exame.getAucPulmonarRonco());
		call.setInt(13, exame.getAucPulmonarSibilo());
		call.setInt(14, exame.getSopro());
		call.setInt(15, exame.getAucCardiaca());
		call.setDate(16, exame.getDataExame());
		call.setInt(17, exame.getFoco());
		call.setInt(18, exame.getIdUsuario());
		call.setInt(19, exame.getIdPaciente());
		
		if(exame.getId() > 0)
			call.setInt(20, exame.getId());
	
		return call; 		
	}
	
	public static ExameFisico setInstance(ResultSet res) throws SQLException
	{
		ExameFisico exFisico = new ExameFisico();	
		
		exFisico.setId(res.getInt("id_exame_fisico"));
		exFisico.setFundoOlhos(res.getInt("fundos_olhos"));
		exFisico.setMembroInfNormal(res.getInt("membro_inf_normal"));
		exFisico.setMembroInfEdema(res.getInt("membro_inf_edema"));
		exFisico.setMembroInfVarizes(res.getInt("membro_inf_varizes"));
		exFisico.setAusenciaPulso(res.getInt("ausencia_pulso"));
		exFisico.setFemurais(res.getInt("femurais"));
		exFisico.setPopliteos(res.getInt("popliteos"));
		exFisico.setTribalPosterior(res.getInt("tribal_posterior"));
		exFisico.setDorsalisPedis(res.getInt("dorsalis_pedis"));
		exFisico.setUlceras(res.getInt("ulceras"));
		exFisico.setAucPulmonarMv(res.getInt("auc_pulmonar_mv"));
		exFisico.setAucPulmonarRonco(res.getInt("auc_pulmonar_ronco"));
		exFisico.setAucPulmonarSibilo(res.getInt("auc_pulmonar_sibilo"));
		exFisico.setSopro(res.getInt("sopro"));
		exFisico.setAucCardiaca(res.getInt("auc_cardiaca"));
		exFisico.setDataExame(res.getDate("data_exame"));
		exFisico.setFoco(res.getInt("foco"));
		exFisico.setIdUsuario(res.getInt("id_usuario"));
		exFisico.setIdPaciente(res.getInt("id_paciente"));
		exFisico.setPaciente(PacienteDAO.LoadObject(res.getInt("id_paciente")));
		exFisico.setUsuario(UsuarioDAO.LoadObject(res.getInt("id_usuario")));
		
		return exFisico;
	}	
}
